package com.dc.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dc.entity.Admin;
import com.dc.service.AdminService;
import com.dc.service.impl.AdminServiceImpl;


/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/AdminLoginServlet")
public class AdminLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	AdminService adminService = new AdminServiceImpl();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 起始位置设置请求编码
		request.setCharacterEncoding("utf-8");
		
		//为了区分不同的请求，设置一个临时变量，根据不同变量的值来执行不同的方法
		String op = ""; //赋值为""
		if (request.getParameter("op")!=null) {
			op = request.getParameter("op");
		}
		//用户登录
		if ("admin_login".equals(op)) {
			doLogin(request, response);
		}
	
	}

	private void doLogin(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取表单的内容，使用request对象的方法request.getParameter("参数名");
		String adminname = request.getParameter("adminname");
		System.out.println("adminname:" + adminname);
		String adminpsw = request.getParameter("adminpsw");
		System.out.println("adminpsw:" + adminpsw);
		Admin admin = adminService.login(adminname, adminpsw);
		System.out.println(admin);
		//获取输入的验证码
		String YZM=request.getParameter("YZM");//自己输入的验证码
		String randStr=(String) request.getSession().getAttribute("randStr");//随机生成的验证码
		if (YZM==null || !YZM.equals(randStr)) {//进行比对
			request.setAttribute("error", "验证码错误");
			request.getRequestDispatcher("admin_login.jsp").forward(request, response);
			return;//跳出该程序，不执行下面的代码
		}

		if (admin != null) {
			request.getSession().setAttribute("Admin", admin);
			//用户选择记住密码
			if (request.getParameter("cksremember") != null) {
				//创建cookie，并设置其过期时间，存储到本地
				Cookie cookie1 = new Cookie("adminname", adminname);
				cookie1.setMaxAge(60 * 60);// 1h
				
				Cookie cookie2 = new Cookie("adminpsw", adminpsw);
				cookie2.setMaxAge(60 * 60);
				//将cookie的信息存储在本地: addCookie
				response.addCookie(cookie1);
				response.addCookie(cookie2);
			}
			//成功
			request.getRequestDispatcher("index.jsp").forward(request, response);				
		}else {
			//失败
			response.sendRedirect("admin_login.jsp");
		}
	}
		
		// 假设用户名为admin，密码为123456，实际上用户名和密码要去调用dao相关方法
//		if (adminname != null) {
//			AdminDao adminDao = new AdminDaoImpl();
//			boolean flag = adminDao.doLogin(adminname, adminpsw);
//			if (flag) {
//				//将用户登录信息存储到session中
//				request.getSession().setAttribute("adminname", adminname);
//				
//				//成功
//				request.getRequestDispatcher("admin_manager.jsp").forward(request, response);
//			} else {
//				//失败
//				response.sendRedirect("admin_login.jsp");
//			}
//		}else {
//			System.out.println("用戶名不能为空！");
//		}
	

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}

}
